﻿using Microsoft.AspNetCore.Mvc;
using ProjectScheduling_web.dao;
using ProjectScheduling_web.model;

namespace ProjectScheduling_web.Controllers.Staff
{
    public class ProjectStaffController : BController
    {
        /// <summary>
        /// 检验员工是否重复
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public ActionResult CheckStaffs(string ids, DateTime todate)
        {
            var arrIds = ids.Split([',']).Select(it => Convert.ToInt32(it));
            var start = DateTime.Today;
            var end = todate.Date;
            var lst = PreparedDataDao.Base.FindAll(z => z.FromDate <= end && z.ToDate >= start && z.PrepardStaffs != null && z.PrepardStaffs.Any(it => arrIds.Contains(it.StaffId)));
            if (lst.Count != 0)
            {
                var lst1 = lst.SelectMany(it => it.PrepardStaffs ?? []).Where(it => arrIds.Contains(it.StaffId)).Select(it => it.Content?.StaffName ?? "").Distinct().ToList();
                return Fail($"{string.Join(',', lst1)}与当前项目有时间冲突");
            }
            return Success("测试通过");
        }
        /// <summary>
        /// 提交认领信息
        /// </summary>
        /// <param name="project_id">项目ID</param>
        /// <param name="staff_ids">团队成员ID集合</param>
        /// <param name="end_time">结束时间</param>
        /// <returns></returns>
        public ActionResult AddPrepared(int project_id, string staff_ids, DateTime end_time)
        {
            if (project_id == 0) return Fail("请选择项目信息");
            if (string.IsNullOrEmpty(staff_ids)) return Fail("请选择团队成员");
            if (end_time < DateTime.Today) return Fail("结束日期为今天以后的日期");
            var project = ProjectsDao.Base.Find(project_id);
            if (project == null) return Fail("当前项目不存在");
            if (project.State != Enum_ProjectState.待排期 && project.State != Enum_ProjectState.挂起) return Fail("当前项目不能排期");
            if (project.State == Enum_ProjectState.待排期)
            {
                project.State = Enum_ProjectState.已排期;
                project.FromDate = DateTime.Now.Date;
                project.ToDate = end_time.Date;

                ProjectsDao.Base.Update(project, it => new { it.State, it.FromDate, it.ToDate });
            }
            else
            {
                project.State = Enum_ProjectState.已排期;
                project.ToDate = end_time.Date;
                ProjectsDao.Base.Update(project, it => new { it.State, it.ToDate });
            }
            ProjectStaffDao.Base.Delete(it => it.ProjectId == project_id);
            var prepare = new PreparedData
            {
                CreateTime = DateTime.Now,
                FromDate = DateTime.Today,
                ToDate = end_time.Date,
                Days = (int)(end_time.Date - DateTime.Today).TotalDays + 1,
                ProjectId = project_id,
                IsNew = true,
                IsEnd = false
            };
            var prepareid = PreparedDataDao.Base.Add(prepare);
            foreach (var id in staff_ids.Split([',']).Select(it => int.Parse(it)))
            {
                var staff = StaffDao.Base.Find(id);
                if (staff != null)
                {
                    ProjectStaffDao.Base.Add(new ProjectStaff
                    {
                        ProjectId = project_id,
                        UserId = id,
                        UserName = staff.TrueName
                    });
                    PreparedStaffDao.Base.Add(new PreparedStaff
                    {
                        PreparedId = prepareid,
                        StaffId = staff.Id,
                        Days = (int)(end_time.Date - DateTime.Today).TotalDays + 1,
                        Content = new PrepardStaffContent
                        {
                            ProjectId = project_id,
                            ProjectName = project.Name,
                            FromDate = DateTime.Today,
                            ToDate = end_time.Date,
                            StaffName = staff.TrueName
                        }
                    });
                }
            }
            return Success("提交成功");
        }
    }
}
